package com.zidian.teacher.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zidian.teacher.R;
import com.zidian.teacher.bean.EvaluateFeedBean;
import com.zidian.teacher.common.Constant;
import com.zidian.teacher.utils.GetUserInfo;
import com.zidian.teacher.utils.ProgressBarBlackUtil;
import com.zidian.teacher.utils.ToastUtil;
import com.zidian.teacher.view.BarView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;

import com.zidian.teacher.common.ToolBarActivity;
import com.zidian.teacher.view.BarBean;

/**
 * Created by Administrator on 2016/9/9 0009.
 */
public class EvaluateFeedBackActivity extends ToolBarActivity {

    @Bind(R.id.evaluate_my_scored)
    TextView evaluateMyScored;
    @Bind(R.id.evaluate_detail)
    LinearLayout evaluateDetail;
    @Bind(R.id.bar_view)
    BarView barView;
    @Bind(R.id.evaluate_average_school)
    TextView evaluateAverageSchool;
    @Bind(R.id.evaluate_average_department)
    TextView evaluateAverageDepartment;
    @Bind(R.id.evaluate_average_ranking)
    TextView evaluateAverageRanking;

    private List<BarBean> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setToolbarView(R.layout.toolbar_left);
        setContentView(R.layout.activity_evaluate_feedback);
        setTitle("教学反馈");
        ButterKnife.bind(this);
//        initDate();
        getMyEvaluation();
        initView();

    }


    private void initView() {

        evaluateDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(EvaluateDetailActivity.class);
            }
        });


    }

    /**
     * 设置数据
     * @param model
     */
    private void initDate(EvaluateFeedBean model) {

        BarBean barBean1 = new BarBean(model.getData().getStudentSchoolAvg(), model.getData().getStudentCollegeAvg());
        BarBean barBean2 = new BarBean(model.getData().getSupervisorSchoolAvg(), model.getData().getSupervisorCollegeAvg());
        BarBean barBean3 = new BarBean(model.getData().getColleagueSchoolAvg(), model.getData().getColleagueCollegeAvg());
        list.add(barBean1);
        list.add(barBean2);
        list.add(barBean3);
        barView.setData(list);


        evaluateMyScored.setText(model.getData().getTeacherAvg() + "");
        evaluateAverageSchool.setText(model.getData().getSchoolAvg() + "");
        evaluateAverageDepartment.setText(model.getData().getCollegeAvg() + "");
        evaluateAverageRanking.setText(model.getData().getTeaRanking() + "");


    }


    private void getMyEvaluation() {
        String loginUrl = Constant.URL_TEACHER + "EvaluateTeacher/forMyEvaluation";
        HashMap<String, String> map = new HashMap<>();
        map.put("teacherId", GetUserInfo.getTeacherId(context));
        map.put("token", GetUserInfo.getToken(context));

        ProgressBarBlackUtil.loadShow(context);

        OkHttpUtils.post().url(loginUrl).params(map).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                ToastUtil.showToast(context, "失败！");
            }

            @Override
            public void onResponse(String response, int id) {
                Log.e("T", response);
                ProgressBarBlackUtil.loadCancel();
                try {
                    JSONObject jsonObject = null;
                    jsonObject = new JSONObject(response);
                    int resultCode = jsonObject.getInt("code");

                    if (resultCode != 200) {

                        ToastUtil.showToast(context, jsonObject.getString("message"));
                        return;
                    } else {
                    }

                } catch (JSONException e) {

                }
                Gson gson = new Gson();
                EvaluateFeedBean model = gson.fromJson(response, EvaluateFeedBean.class);
                if (model.isSuccess()) {
                    initDate(model);
                }
            }
        });
    }


}
